package MyConnectionPool;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class TestConPool {
    public static void main(String[] args) {
        //获取线程池实例
        final ConPool pool = MyConPool.PoolEnum.INSTANCE.getPoolInstance();
        for (int i = 0; i < 1000; i++) {
            //创建11个线程//可以使用JDK自带的线程池ExecutorService创建 new Thread性能开销大
            new Thread(() -> {
                Connection conn = null;
                try {
                    conn = pool.getConn();
                    //执行操作
                    PreparedStatement preparedStatement =
                            conn.prepareStatement("select COUNT(title) c from video");
                    ResultSet resultSet = preparedStatement.executeQuery();
                    System.out.println(resultSet.next());
                    System.out.println(resultSet.getObject("c"));
                } catch (SQLException e) {
                    e.printStackTrace();
                } finally {
                    if (conn != null) {
                        pool.release(conn); //关闭连接
                    }
                }
            }).start();
        }
    }
}
